<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class EnvInfoController extends Controller
{
    //最近一次的环境情况
    public function lastInfo(Request $request) {
        if($request->expectsJson()) {
            $envIds = DB::table('a_env_info as a')
                        ->leftJoin('a_hb6303 as b', 'b.id', '=', 'a.hb6303_id')
                        ->selectRaw('MAX(a.id) AS max_id')
                        ->groupBy('b.id')->pluck('max_id')->toArray();
            $data['list'] = DB::table('a_env_info as a')
                              ->leftJoin('a_hb6303 as b', 'b.id', '=', 'a.hb6303_id')
                              ->whereIn('a.id', $envIds)
                              ->select('a.temp', 'a.humi', 'a.pressure', 'b.pos')
                              ->get();
            return out(1, '', $data);
        }
    }

    //最近n天的环境情况
    public function lastNInfo() {
        //        if($num === 0 || $num > 7) {
        //            return out(0, '查询天数必须 >0且<8 天');
        //        }
        $data = [];
        $date = date('Y-m-d', strtotime('-7 days'));
        $hb6303 = DB::Table('a_hb6303')->pluck('pos', 'dev_num');
        $hb6303Lst = [];
        foreach($hb6303 as $k => $pos) {
            $hb6303Lst[$k] = [
                'pos'     => $pos,
                'options' => [
                    'temp'  => [],
                    'humi'  => [],
                    'press' => [],
                    'time'  => [],
                ],
            ];
        }
        $list = DB::table('a_env_info')
                  ->whereDate('time', '>', $date)
                  ->orderBy('time', 'asc')
                  ->select('dev_num', 'temp', 'humi', 'pressure', 'time')
                  ->get();
        foreach($list as $item) {
            array_push($hb6303Lst[$item->dev_num]['options']['temp'], $item->temp);
            array_push($hb6303Lst[$item->dev_num]['options']['humi'], $item->humi);
            array_push($hb6303Lst[$item->dev_num]['options']['press'], $item->pressure);
            array_push($hb6303Lst[$item->dev_num]['options']['time'], $item->time);
        }
        $data['list'] = $hb6303Lst;
        return out(1, 'success', $data);
    }
    
    //山东仁科modbus数据利用nodejs数据上传
    public function add(){
        info("山东仁科modbus数据利用nodejs数据上传");
        info(request()->all());
        $dev_num = request('dev_num','');
        if (!$dev_num) {
            return '缺少设备编号！';
        }
        $hexData = request('hex_data',''); //如果上传的是16进制数据
        $res = 0;
        if (strlen($hexData) == 22) {
            $humi = hexdec(substr($hexData,6,4))/10;
            // info(substr($hexData,6,4));
            $temp = strtolower(substr($hexData,10,2)) == 'ff' ? hexdec(substr($hexData,12,2))/10 : hexdec(substr($hexData,10,4))/10;
            // info(substr($hexData,10,4));
            $pressure = hexdec(substr($hexData,14,4))/10;
            if ($humi || $temp || $pressure ) {
                $hb6303_id = DB::table('a_hb6303')->where('dev_num',$dev_num)->value('id');
                $data = [
                    'temp' => number_format($temp,1,".",""),
                    'humi' => number_format($humi,1,".",""),
                    'pressure' => number_format($pressure,2,".",""),
                    'dev_num'=>$dev_num,
                    'hb6303_id'=>$hb6303_id,
                ];
                $res = DB::table('a_env_info')->insert($data);
            }
            // return 1;
        }
        return $res ? 1 : 0;
    }
}
